package com.ailight.smart.ai.facade.impl;

import cn.hutool.json.JSONUtil;
import com.ailight.smart.ai.assembler.AiModelAssembler;
import com.ailight.smart.ai.command.AiCommand;
import com.ailight.smart.ai.domain.dto.deepseek.DeepSeekParamDTO;
import com.ailight.smart.ai.enums.AiModelEnum;
import com.ailight.smart.ai.facade.AiModelFacade;
import com.ailight.smart.ai.factory.AiModelFactory;
import com.ailight.smart.ai.strategy.AiModelStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.stereotype.Component;


/**
 * @author: 朱海鹏
 * @Package: com.ailight.smart.ai.facade.impl
 * @Project: ai-light
 * @date: 2025/3/19 10:58
 * @description :
 */
@Component
@RequiredArgsConstructor
public class AiModelFacadeImpl implements AiModelFacade {
    private final OpenAiChatClient openAiChatClient;


    private final AiModelFactory aiModelFactory;

    @Override
    public Object getModelAnswer(AiCommand command) {
        // 从工厂中获取对应的Ai大模型策略
        AiModelStrategy aiModelStrategy = aiModelFactory.getAiModelStrategy(AiModelEnum.getModelEnum(command.getModelCode()));
        // 进行模型回答 这里主要是获取不同模型的枚举
        AiModelEnum modelEnum = AiModelEnum.getModelEnum(command.getModelCode());
        //这边进行转参
        DeepSeekParamDTO param = AiModelAssembler.buldeDpParam(command, modelEnum);
        if (Boolean.FALSE.equals(command.getStream())) {
            // 这边是非流式
            return aiModelStrategy.getResultNoStream(JSONUtil.toJsonStr(param), modelEnum.getR(), modelEnum);
        } else {
            // 流式返回
            return aiModelStrategy.getSseEmitter(JSONUtil.toJsonStr(param), modelEnum);
        }
    }

    @Override
    public String chat(String msg) {
        return openAiChatClient.call(msg);
    }
}
